package org.iteam.xlz.core.service.ad;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.ad.ActivityInfo;

public class ActivityInfoService extends BaseService<ActivityInfo>{
	private static ActivityInfoService instance;
	
	private ActivityInfoService(){}
	
	public static ActivityInfoService getInstance(){
		if(null==instance){
			instance=new ActivityInfoService();
		}
		return instance;
	}
	
	public ActivityInfo findOne(String aid){
		try {
			return super.show("select * from activityinfo where 1=1 and aid=?",aid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ActivityInfo();
		}
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from activityinfo where 1=1 ");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} 
	}
	
	public List<ActivityInfo> query(long page,long rows){
		try {
			return super.query("select * from activityinfo where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<ActivityInfo>();
		}
	}
	
	public boolean create(ActivityInfo model){
		SqlBuilder sql=SqlBuilder.createInsert("activityinfo");
		sql.addField("aid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("actname",model.getActname());
		sql.addField("begintime",model.getBegintime());
		sql.addField("endtime",model.getEndtime());
		sql.addField("contentType",model.getContentType());
		if(StringUtils.isNotBlank(model.getContent())){
			sql.addField("content",model.getContent());
		}
		sql.addField("isRecommend",model.getIsRecommend());
		sql.addField("os",model.getOs());
		sql.addField("status",model.getStatus());
		sql.addField("createTime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		sql.addField("imgurl",model.getImgurl());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(ActivityInfo model){
		SqlBuilder sql=SqlBuilder.createUpdate("activityinfo");
		sql.addField("actname",model.getActname());
		sql.addField("begintime",model.getBegintime());
		sql.addField("endtime",model.getEndtime());
		sql.addField("contentType",model.getContentType());
		if(StringUtils.isNotBlank(model.getContent())){
			sql.addField("content",model.getContent());
		}
		sql.addField("isRecommend",model.getIsRecommend());
		sql.addField("os",model.getOs());
		sql.addField("status",model.getStatus());
		sql.addField("imgurl",model.getImgurl());
		sql.addWhere("aid",model.getAid());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String id){
		try {
			return super.updates("delete from activityinfo where aid=?",id)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
